home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch11 / BSpline.frm (.txt) < prev    next >
Visual Basic Form  |  1999-06-12  |  9KB  |  310 lines

  1. VERSION 5.00
  2. Begin VB.Form frmBspline 
  3.    Caption         =   "BSpline"
  4.    ClientHeight    =   5430
  5.    ClientLeft      =   2175
  6.    ClientTop       =   645
  7.    ClientWidth     =   4830
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   362
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   322
  13.    Begin VB.CheckBox chkShowT 
  14.       Caption         =   "Show t Values"
  15.       Height          =   255
  16.       Left            =   1680
  17.       TabIndex        =   8
  18.       Top             =   300
  19.       Width           =   1755
  20.    End
  21.    Begin VB.TextBox txtK 
  22.       Height          =   285
  23.       Left            =   1140
  24.       TabIndex        =   6
  25.       Text            =   "3"
  26.       Top             =   45
  27.       Width           =   375
  28.    End
  29.    Begin VB.CommandButton cmdNew 
  30.       Caption         =   "New"
  31.       Enabled         =   0   'False
  32.       Height          =   375
  33.       Left            =   4320
  34.       TabIndex        =   5
  35.       Top             =   0
  36.       Width           =   495
  37.    End
  38.    Begin VB.CommandButton cmdGo 
  39.       Caption         =   "Go"
  40.       Default         =   -1  'True
  41.       Enabled         =   0   'False
  42.       Height          =   375
  43.       Left            =   3600
  44.       TabIndex        =   4
  45.       Top             =   0
  46.       Width           =   495
  47.    End
  48.    Begin VB.CheckBox chkControlPoints 
  49.       Caption         =   "Show Control Points"
  50.       Height          =   255
  51.       Left            =   1680
  52.       TabIndex        =   3
  53.       Top             =   0
  54.       Value           =   1  'Checked
  55.       Width           =   1755
  56.    End
  57.    Begin VB.TextBox txtDt 
  58.       Height          =   285
  59.       Left            =   240
  60.       TabIndex        =   2
  61.       Text            =   "0.05"
  62.       Top             =   45
  63.       Width           =   615
  64.    End
  65.    Begin VB.PictureBox picCanvas 
  66.       AutoRedraw      =   -1  'True
  67.       Height          =   4815
  68.       Left            =   0
  69.       ScaleHeight     =   317
  70.       ScaleMode       =   3  'Pixel
  71.       ScaleWidth      =   317
  72.       TabIndex        =   0
  73.       Top             =   600
  74.       Width           =   4815
  75.    End
  76.    Begin VB.Label Label1 
  77.       Caption         =   "K"
  78.       Height          =   255
  79.       Index           =   0
  80.       Left            =   960
  81.       TabIndex        =   7
  82.       Top             =   60
  83.       Width           =   255
  84.    End
  85.    Begin VB.Label Label1 
  86.       Caption         =   "dt"
  87.       Height          =   255
  88.       Index           =   1
  89.       Left            =   0
  90.       TabIndex        =   1
  91.       Top             =   60
  92.       Width           =   255
  93.    End
  94. Attribute VB_Name = "frmBspline"
  95. Attribute VB_GlobalNameSpace = False
  96. Attribute VB_Creatable = False
  97. Attribute VB_PredeclaredId = True
  98. Attribute VB_Exposed = False
  99. Option Explicit
  100. Private Const GAP = 2
  101. ' The endpoints are points 1 and 4. The control
  102. ' points are points 2 and 3.
  103. Private MaxPt As Integer
  104. Private PtX() As Single
  105. Private PtY() As Single
  106. Private MakingNew As Boolean
  107. ' The index of the point being dragged.
  108. Private Dragging As Integer
  109. ' Kvalue determines the smoothness of the curve.
  110. Private Kvalue As Integer
  111. ' t runs between 0 and MaxPt - Kvalue + 2.
  112. Private MaxT As Single
  113. ' Recursively compute the blending function.
  114. Private Function Blend(ByVal i As Integer, ByVal k As Integer, ByVal t As Single) As Single
  115. Dim numer As Single
  116. Dim denom As Single
  117. Dim value1 As Single
  118. Dim value2 As Single
  119.     ' Base case for the recursion.
  120.     If k = 1 Then
  121.         If (Knot(i) <= t) And (t < Knot(i + 1)) Then
  122.             Blend = 1
  123.         ElseIf (t = MaxT) And (Knot(i) <= t) And (t <= Knot(i + 1)) Then
  124.             Blend = 1
  125.         Else
  126.             Blend = 0
  127.         End If
  128.         Exit Function
  129.     End If
  130.     denom = Knot(i + k - 1) - Knot(i)
  131.     If denom = 0 Then
  132.         value1 = 0
  133.     Else
  134.         numer = (t - Knot(i)) * Blend(i, k - 1, t)
  135.         value1 = numer / denom
  136.     End If
  137.     denom = Knot(i + k) - Knot(i + 1)
  138.     If denom = 0 Then
  139.         value2 = 0
  140.     Else
  141.         numer = (Knot(i + k) - t) * Blend(i + 1, k - 1, t)
  142.         value2 = numer / denom
  143.     End If
  144.     Blend = value1 + value2
  145. End Function
  146. ' Draw the curve on the indicated picture box.
  147. Private Sub DrawCurve(pic As PictureBox, start_t As Single, stop_t As Single, dt As Single)
  148. Dim t As Single
  149.     pic.Cls
  150.     pic.CurrentX = X(start_t)
  151.     pic.CurrentY = Y(start_t)
  152.     t = start_t + dt
  153.     Do While t < stop_t
  154.         pic.Line -(X(t), Y(t))
  155.         t = t + dt
  156.     Loop
  157.     pic.Line -(X(stop_t), Y(stop_t))
  158. End Sub
  159. ' Return the ith knot value.
  160. Private Function Knot(ByVal i As Integer) As Integer
  161.     If i < Kvalue Then
  162.         Knot = 0
  163.     ElseIf i <= MaxPt Then
  164.         Knot = i - Kvalue + 1
  165.     Else
  166.         Knot = MaxPt - Kvalue + 2
  167.     End If
  168. End Function
  169. ' The parametric function Y(t).
  170. Private Function Y(ByVal t As Single) As Single
  171. Dim i As Integer
  172. Dim value As Single
  173.     For i = 0 To MaxPt
  174.         value = value + PtY(i) * Blend(i, Kvalue, t)
  175.     Next i
  176.     Y = value
  177. End Function
  178. ' The parametric function X(t).
  179. Private Function X(ByVal t As Single) As Single
  180. Dim i As Integer
  181. Dim value As Single
  182.     For i = 0 To MaxPt
  183.         value = value + PtX(i) * Blend(i, Kvalue, t)
  184.     Next i
  185.     X = value
  186. End Function
  187. ' Use DrawCurve to draw the Bezier curve.
  188. Private Sub DrawBspline()
  189. Dim dt As Single
  190. Dim i As Integer
  191. Dim oldstyle As Integer
  192.     If MaxPt < 0 Then Exit Sub
  193.     MousePointer = vbHourglass
  194.     Kvalue = CInt(txtK.Text)
  195.     dt = CSng(txtDt.Text)
  196.     MaxT = MaxPt - Kvalue + 2
  197.     DrawCurve picCanvas, 0, MaxT, dt
  198.     If chkControlPoints.value = vbChecked Then
  199.         ' Draw the control points.
  200.         For i = 0 To MaxPt
  201.             picCanvas.Line _
  202.                 (PtX(i) - GAP, PtY(i) - GAP)- _
  203.                 Step(2 * GAP, 2 * GAP), , BF
  204.         Next i
  205.         
  206.         ' Connect the control points.
  207.         oldstyle = picCanvas.DrawStyle
  208.         picCanvas.DrawStyle = vbDot
  209.         picCanvas.CurrentX = PtX(0)
  210.         picCanvas.CurrentY = PtY(0)
  211.         For i = 1 To MaxPt
  212.             picCanvas.Line -(PtX(i), PtY(i))
  213.         Next i
  214.         picCanvas.DrawStyle = oldstyle
  215.     End If
  216.     ' Mark the t values if desired.
  217.     If chkShowT.value = vbChecked Then
  218.         For dt = 0 To MaxT Step 1#
  219.             picCanvas.Line (X(dt), Y(dt) - 5)-Step(0, 10)
  220.             picCanvas.Line (X(dt) - 5, Y(dt))-Step(10, 0)
  221.         Next dt
  222.     End If
  223.     MousePointer = vbDefault
  224. End Sub
  225. ' Either collect a new point or select a point and
  226. ' start dragging it.
  227. Private Sub picCanvas_MouseDown(button As Integer, Shift As Integer, X As Single, Y As Single)
  228. Dim i As Integer
  229.     ' If we are selecting points, do so now.
  230.     If MakingNew Then
  231.         MaxPt = MaxPt + 1
  232.         ReDim Preserve PtX(0 To MaxPt)
  233.         ReDim Preserve PtY(0 To MaxPt)
  234.         PtX(MaxPt) = X
  235.         PtY(MaxPt) = Y
  236.         picCanvas.Line _
  237.             (X - GAP, Y - GAP)- _
  238.             Step(2 * GAP, 2 * GAP), , BF
  239.         
  240.         If MaxPt >= 2 Then cmdGo.Enabled = True
  241.         
  242.         Exit Sub
  243.     End If
  244.     ' Otherwise start dragging a point.
  245.     ' Find a close point.
  246.     For i = 0 To MaxPt
  247.         If Abs(PtX(i) - X) <= GAP And _
  248.            Abs(PtY(i) - Y) <= GAP Then Exit For
  249.     Next i
  250.     If i > MaxPt Then Exit Sub
  251.     Dragging = i
  252.     picCanvas.DrawMode = vbInvert
  253.     PtX(Dragging) = X
  254.     PtY(Dragging) = Y
  255.     picCanvas.Line _
  256.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  257.         Step(2 * GAP, 2 * GAP), , BF
  258. End Sub
  259. ' Continue dragging a point.
  260. Private Sub picCanvas_MouseMove(button As Integer, Shift As Integer, X As Single, Y As Single)
  261.     If Dragging < 0 Then Exit Sub
  262.     picCanvas.Line _
  263.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  264.         Step(2 * GAP, 2 * GAP), , BF
  265.     PtX(Dragging) = X
  266.     PtY(Dragging) = Y
  267.     picCanvas.Line _
  268.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  269.         Step(2 * GAP, 2 * GAP), , BF
  270. End Sub
  271. ' Finish the drag and redraw the curve.
  272. Private Sub picCanvas_MouseUp(button As Integer, Shift As Integer, X As Single, Y As Single)
  273.     If Dragging < 0 Then Exit Sub
  274.     picCanvas.DrawMode = vbCopyPen
  275.     PtX(Dragging) = X
  276.     PtY(Dragging) = Y
  277.     Dragging = -1
  278.     DrawBspline
  279. End Sub
  280. Private Sub CmdGo_Click()
  281.     MakingNew = False
  282.     cmdNew.Enabled = True
  283.     DrawBspline
  284. End Sub
  285. ' Prepare to get new points.
  286. Private Sub CmdNew_Click()
  287.     MaxPt = -1
  288.     cmdGo.Enabled = False
  289.     cmdNew.Enabled = False
  290.     MakingNew = True
  291.     picCanvas.Cls
  292. End Sub
  293. Private Sub chkControlPoints_Click()
  294.     DrawBspline
  295. End Sub
  296. Private Sub Form_Load()
  297.     MakingNew = True
  298.     MaxPt = -1
  299.     Dragging = -1
  300. End Sub
  301. ' Make the picCanvas as big as possible.
  302. Private Sub Form_Resize()
  303.     picCanvas.Move 0, picCanvas.Top, _
  304.         ScaleWidth, ScaleHeight - picCanvas.Top
  305.     DrawBspline
  306. End Sub
  307. Private Sub chkShowT_Click()
  308.     DrawBspline
  309. End Sub
  310.